<?php

/**
 * SimplifyPHP Framework
 *
 * This file is part of SimplifyPHP Framework.
 *
 * SimplifyPHP Framework is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * SimplifyPHP Framework is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @author Rodrigo Rutkoski Rodrigues, <rutkoski@gmail.com>
 */

/**
 * Application Controller
 * 
 * @author Rodrigo Rutkoski Rodrigues <rutkoski@gmail.com>
 * @package Simplify_Kernel_Controller
 */
class ApplicationControllerImpl implements IApplicationController
{

  /**
   * 
   * @var Config
   */
  protected $config;

  /**
   * 
   * @var IRequest
   */
  protected $request;

  /**
   * 
   * @var IResponse
   */
  protected $response;

  /**
   * 
   * @var IRouter
   */
  protected $router;

  /**
   * 
   * @var Language
   */
  protected $l10n;

  /**
   * 
   * @param $config
   * @return unknown_type
   */
  public function __construct(Config $config)
  {
    $this->config = $config;
  }

  public function initialize() {}

  /**
   *
   */
  public function getL10n()
  {
    return $this->l10n;
  }
  public function setL10n($l10n)
  {
    $this->l10n = $l10n;
  }

  /**
   *
   */
  public function getConfig()
  {
    return $this->config;
  }
  public function setConfig(Config $config)
  {
    $this->config = $config;
  }

  /**
   *
   */
  public function getRouter()
  {
    return $this->router;
  }
  public function setRouter(IRouter $router)
  {
    $this->router = $router;
  }

  /**
   *
   */
  public function getRequest()
  {
    return $this->request;
  }
  public function setRequest(IRequest $request)
  {
    $this->request = $request;
  }

  /**
   *
   */
  public function getResponse()
  {
    return $this->response;
  }
  public function setResponse(IResponse $response)
  {
    $this->response = $response;
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/api/IApplicationController#dispatch()
   */
  public function dispatch()
  {
    $route = Simplify::getRequest()->get('route');
    return $this->forward($route);
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/core/IApplicationController#forward($route)
   */
  public function forward($route)
  {
    $info = Simplify::getRouter()->parse($route);
    
    $cacheid = Simplify::getConfig()->get('locale') . $route;
    
    $cache = new FileCache(Simplify::getConfig()->get('{env}:cache:dir'));
    
    $cacheEnabled = Simplify::getConfig()->get('{env}:cache:enabled', true);
    
    $configFile = sy_fix_path($info->get('path') . '/' . $info->get('controller') . '.yml');
    if ($cacheEnabled && file_exists($configFile)) {
      $config = new Config();
      $config->copyAllFromYAMLFile($configFile);
      $cacheEnabled = $config->get('disable_cache') !== true && $config->get('actions:' . $info->get('action') . ':disable_cache') !== true;
    }
    
    if ($cacheEnabled && $cache->cached($cacheid)) {
      $output = $cache->read($cacheid);
    }
    
    else {
      $Controller = $this->factory($info->get('controller'), $info->get('path'));
      $output = $Controller->callActionArray($info->get('action'), $info->get('params'));
      
      if (Simplify::getConfig()->get('{env}:cache:enabled', true) && $Controller->cacheEnabled) {
        $cache->write($cacheid, $output, $Controller->cacheTimeout);
      }
    }
    
    return Simplify::getResponse()->output($output);
  }

  /**
   * Factory an Action Controller object.
   * 
   * @param string $name controller name
   * @return ActionController
   */
  protected function factory($controller, $path)
  {
    $class = Inflector::camelize($controller . '_controller');
    $Controller = new $class($path);
    return $Controller;
  }

}

?>